package com.tzstcl.base.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.tzstcl.commons.utils.LongJsonDeserializer;
import com.tzstcl.commons.utils.LongJsonSerializer;
import com.tzstcl.commons.utils.SnowflakeIdWorker;
import com.tzstcl.framework.config.SysConfig;
import com.tzstcl.framework.constants.AppConstants;
import com.tzstcl.framework.shiro.ShiroUtils;
import com.tzstcl.gen.util.CamelUtils;
import com.tzstcl.sys.user.model.SysUser;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Entity基类
 *
 * @author tzstcl
 */
public abstract class BaseModel<T extends BaseModel> implements Serializable {

    /**
     * id
     */
    @JsonSerialize(using = LongJsonSerializer.class)
    @JsonDeserialize(using = LongJsonDeserializer.class)
    private Long id;
    /**
     * 创建人
     */
    @JsonIgnore
    private String createBy;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 更新人
     */
    @JsonIgnore
    private String updateBy;
    /**
     * 更新时间
     */
    private Date updateTime;
    /**
     * 删除标记
     */
    @JsonIgnore
    private String delFlag;
    /**
     * 备注
     */
    private String remarks;
    /**
     * 排序字段
     */
    @JsonIgnore
    private String sortName;
    /**
     * 排序方式
     */
    @JsonIgnore
    private String sortOrder;
    /**
     * 分页（当前页）
     */
    @JsonIgnore
    private Integer pageNum;
    /**
     * 分页（页面大小）
     */
    @JsonIgnore
    private Integer pageSize;
    /**
     * 请求参数
     */
    @JsonIgnore
    private Map<String, Object> params;

    public BaseModel() {
        pageNum = 1;
        pageSize = SysConfig.getPageSize();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public String getSortName() {
        return sortName;
    }

    public void setSortName(String sortName) {
        this.sortName = sortName;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    /**
     * 初始化默认字段
     */
    public void preInit() {
        SysUser sysUser = ShiroUtils.getUser();
        if (null==this.id) {
            if (sysUser != null) {
                this.createBy = sysUser.getId() + "";
                this.updateBy = sysUser.getId() + "";
            }
            if (null==this.id) {
                this.id = SnowflakeIdWorker.getInstance().nextId();
            }
            this.createTime = new Date();
            this.updateTime = new Date();
            this.setDelFlag(AppConstants.TABLE_DELFLAG_VALID);
        } else {//更新
            if (sysUser != null) {
                this.updateBy = sysUser.getId() + "";
            }
            this.updateTime = new Date();
        }
    }

    /**
     * 检查排序字段
     */
    public void preSort(){
        if(this.sortName != null){
            if(this.sortName.matches("^[A-Za-z]+$")){
                this.sortName = CamelUtils.camel2Underline(this.sortName);
            }else{
                this.sortName = null;
            }
        }
        if(this.sortOrder != null){
            if(!this.sortOrder.matches("^(asc|ASC|desc|DESC)+$")){
                this.sortOrder = null;
            }
        }
    }

}
